<?php
	//gets info from the given mysql query or the last mysql query.
	function get_mysql_info($linkid = null){
		$linkid? $strInfo = mysql_info($linkid) : $strInfo = mysql_info();
	   
		$return = array();
		ereg("Records: ([0-9]*)", $strInfo, $records);
		ereg("Duplicates: ([0-9]*)", $strInfo, $dupes);
		ereg("Warnings: ([0-9]*)", $strInfo, $warnings);
		ereg("Deleted: ([0-9]*)", $strInfo, $deleted);
		ereg("Skipped: ([0-9]*)", $strInfo, $skipped);
		ereg("Rows matched: ([0-9]*)", $strInfo, $rows_matched);
		ereg("Changed: ([0-9]*)", $strInfo, $changed);
	   
		$return['records'] = $records[1];
		$return['duplicates'] = $dupes[1];
		$return['warnings'] = $warnings[1];
		$return['deleted'] = $deleted[1];
		$return['skipped'] = $skipped[1];
		$return['rows_matched'] = $rows_matched[1];
		$return['changed'] = $changed[1];
	   
		return $return;
	}
	
	function isLoggedIn(){
		session_start();
		return (isset($_SESSION["username"])) ? true : false;
	}
	
	function connectToDB(){
		$dbHost = "localhost:3306";
		$dbName = "webstudio";
		$dbUserName = "root";
		$dbPassword = "admin";
		
		$con = @mysql_pconnect($dbHost, $dbUserName, $dbPassword);
		if(!$con){
			header("location:index.php?message=".urlencode('Could not connect: ' . mysql_error()));
			die();
		}
		
		if(!mysql_select_db($dbName, $con)){
			header("location:index.php?message=".urlencode('Could not connect: ' . mysql_error()));
			die();
		}
		return $con;
	}
	
	//get the user's key from the database given the username
	function getUserId($uname = ""){
		$result = mysql_query("select id from user where username='$uname'");
		if(mysql_num_rows($result)==1){
			$row = mysql_fetch_array($result);
			return $row["id"];
		}else{
			return -1;
		}
	}
	
	//append the string to the log file
	function appendLog($content = ""){
		$handle = fopen("log.txt", "a");
		fwrite($handle, $content . "\n");
		fclose($handle);
	}
	
	//append the info to the log file
	function appendInfo($content = ""){
		$handle = fopen("log.txt", "a");
		date_default_timezone_set("Asia/Manila");
		$timestamp = date("M j, Y, g:i:s a");
		fwrite($handle, "INFO " . $timestamp . "- " . $content . "\n");
		fclose($handle);
	}
	
	//append the debug info to the log file
	function appendDebug($content = ""){
		$handle = fopen("log.txt", "a");
		date_default_timezone_set("Asia/Manila");
		$timestamp = date("M j, Y, g:i:s a");
		fwrite($handle, "DEBUG " . $timestamp . "- " . $content  . "\n");
		fclose($handle);
	}
	
	//reset the log
	function clearLog(){
		$handle = fopen("log.txt", "w");
		fclose($handle);
	}
?>